package com.woyao.core;

import android.app.Activity;
import android.os.AsyncTask;

import com.woyao.WoyaoooApplication;
import com.woyao.core.model.BusinessCategory;
import com.woyao.core.model.GetBusinessCategoryResponse;
import com.woyao.core.service.BusinessCategoryService;
import com.woyao.core.util.ServiceFactory;

import java.io.IOException;
import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by summerwind on 2016-05-24.
 */
public class BusinessCategoryLoader {
    private Activity context;

    private OnSuccessListener listener;

    public BusinessCategoryLoader(Activity context, String parentNo) {
        this.context = context;
        this.parentNo = parentNo;
    }

    public void setOnSuccessListener(OnSuccessListener listener){
        this.listener = listener;
    }

    private String parentNo = "0";

    public void load(){
        AsyncTask<Void,Void,GetBusinessCategoryResponse> task =
                new AsyncTask<Void, Void, GetBusinessCategoryResponse>() {
                    @Override
                    protected GetBusinessCategoryResponse doInBackground(Void... params) {
                        BusinessCategoryService svc = ServiceFactory.get(BusinessCategoryService.class);
                        Call<GetBusinessCategoryResponse> responseCall = svc.getChildCategory(parentNo, WoyaoooApplication.userId);
                        try {
                            GetBusinessCategoryResponse response = responseCall.execute().body();
                            return response;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                    @Override
                    protected void onPostExecute(final GetBusinessCategoryResponse response) {
                            listener.onSuccess(response);
                    }

                    @Override
                    protected void onCancelled() {
                    }
                };
        task.execute();

    }

    public interface OnSuccessListener {
        /**
         * Called when a view has been clicked.
         *
         * @param response The view that was clicked.
         */
        void onSuccess(GetBusinessCategoryResponse response);
    }

    public Activity getContext() {
        return context;
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public String getParentNo() {
        return parentNo;
    }

    public void setParentNo(String parentNo) {
        this.parentNo = parentNo;
    }

    public static ArrayList<BusinessCategory> processLevel(ArrayList<BusinessCategory> categoryList){
        ArrayList<BusinessCategory> local = new ArrayList<>();
        for(BusinessCategory category : categoryList){
            category.setOriginName(category.getName());
            local.add(clone(category));

        }
        ArrayList<BusinessCategory> parentList = new ArrayList<>();
        for(BusinessCategory category : local){
//            category.setChildren(new ArrayList<BusinessCategory>());
            for(BusinessCategory child : local){
                if(category.getNo().equals(child.getParentNo())){
                    child.setParent(category);
                }
            }
        }
        for(BusinessCategory category : local){
            if(category.getParent() == null){
                parentList.add(category);
            }else{
                category.setName(" -- " + category.getName());
            }
            category.setParent(null);
        }
        ArrayList<BusinessCategory> result = new ArrayList<>();
        for(BusinessCategory parent : parentList){
            result.add(parent);
            for(BusinessCategory child : local){
                if(parent.getNo().equals(child.getParentNo())){
                    result.add(child);
                }
            }
        }
        return result;
    }

    private static BusinessCategory clone(BusinessCategory category){
        BusinessCategory clone = new BusinessCategory();
        clone.setName(category.getName());
        clone.setNo(category.getNo());
        clone.setCount(category.getCount());
        clone.setDescription(category.getDescription());
        clone.setImage(category.getImage());
        clone.setParentNo(category.getParentNo());
        clone.setParent(null);
        clone.setChildren(null);
        clone.setOriginName(category.getOriginName());
        return clone;
    }
}
